#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <map>

#include "mzhttp.h"


void show_headers(mz_http_headers* headers){
	printf("===============mz_http_headers==============\n");

	printf("method: %s \n", headers->method);
	printf("url: %s \n", headers->url);
	printf("version: %s \n", headers->version);
	printf("query_string: %s \n", headers->query_string);
	print_args(&headers->args);

	printf("============================================\n");
}

// init and new
mz_args* new_mz_args(){
	// init
	mz_args* args;
	args = (mz_args*)malloc(sizeof(mz_args));
	args->key[0] = '\0';
	args->value[0] = '\0';
	args->next = NULL;
	return args;
}


//delete mz_args(free)
void delete_mz_args(mz_args* args){
	mz_args* tmp = args;
	mz_args* tmp_next = tmp->next;   // fetch next one
	while (tmp_next != NULL){
		tmp = tmp_next;    
		tmp_next = tmp->next;    // check if there is next
		free(tmp);
		printf("free: %p \n", tmp);
	}
}


// use key to get value
char* get_args_value(mz_args* args, const char* key){
	mz_args* tmp = args;
	while (tmp->next != NULL){
		tmp = tmp->next;
		if(strcmp(key, tmp->key) == 0){
			//found!
			return tmp->value;
		}
	}
	return NULL;
}


void print_args(mz_args* args){
	mz_args* tmp = args;
	//printf("key: %s, value: %s, next: %p \n", tmp->key, tmp->value, tmp->next);
	while (tmp->next != NULL){
		tmp = tmp->next;
		printf("key: %s, value: %s, next: %p \n", tmp->key, tmp->value, tmp->next);
	}
}

void print_map(MZ_HEADER_DICTMAP* dict){
	MZ_HEADER_DICTMAP _dict = *dict;  //copy
	MZ_HEADER_DICTMAP::iterator it;
    MZ_HEADER_DICTMAP::iterator itEnd;
    it = _dict.begin();
    itEnd = _dict.end();
    while (it != itEnd) {
		printf(" > key: %s, value: %s \n", it->first.c_str(), it->second.c_str());
		it++;
	}
}




void format_args(char* query_string, mz_args* args){
	unsigned long index = 0;
	//int length_query_string = strlen(query_string);
	
	int is_key = 1;            // flag
	char key_buf[255] = {0};
	char value_buf[255] = {0};
	mz_args* tmp_arg = NULL;
	mz_args* now_arg = args;
	
	args->key[0] = '\0';   // init args(head pointer)
	args->value[0] = '\0';
	args->next = NULL;
	
	
	// check
	if (*query_string == '='){
		printf("err \n");
		return;
	} 
	
	// walk the string
	while ( (*query_string != '\0') && (index < sizeof(key_buf)-1) && (index < sizeof(value_buf)-1)){      // safe(maybe)
		//printf("*query_string: %c \n", *query_string);
		if (is_key && (*query_string == '=')){
			// found '='
			key_buf[index] = '\0';
			index = 0;                   // back to 0
			//printf("key: %s \n", key_buf);
			is_key = 0;            // change to value
		}else if (is_key && (*query_string != '=')){  // to find '='
			key_buf[index] = *query_string;
			index ++;
		}else if (!is_key && (*query_string == '&')){
			// found '&'
			value_buf[index] = '\0';
			index = 0;
			//printf("value: %s \n", value_buf);
			
			is_key = 1;
			
			// save key value!
			tmp_arg = new_mz_args();
			strcpy(tmp_arg->key, key_buf);     // copy
			strcpy(tmp_arg->value, value_buf);   // copy
			now_arg->next = tmp_arg;
			now_arg = tmp_arg;
			
		}else if (!is_key && (*query_string != '&')){    // to find '&'
			value_buf[index] = *query_string;
			index ++;
		}
		query_string ++;
	}
	
	if (!is_key){
		value_buf[index] = '\0';
		index = 0;
		// printf("value: %s \n", value_buf);
		// save key value!
		tmp_arg = new_mz_args();
		strcpy(tmp_arg->key, key_buf);     // copy
		strcpy(tmp_arg->value, value_buf);   // copy
		now_arg->next = tmp_arg;
		now_arg = tmp_arg;
	}

	
	
	// for test
	/*
	printf("===========================format_args==============================\n");
	printf("length_query_string: %d\n", length_query_string);
	print_args(args);
	printf("===========================format_args==============================\n");
	*/
	// delete_mz_args(args); // is need after use
}



